package com.crr.mall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import cn.hutool.core.bean.BeanUtil;
import com.crr.mall.common.bean.ResultBean;
import com.crr.mall.common.utils.BaseController;
import com.crr.mall.product.query.AttrGroupQuery;
import com.crr.mall.product.vo.AttrGroupRelationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.crr.mall.product.entity.AttrGroupEntity;
import com.crr.mall.product.service.AttrGroupService;
import com.crr.mall.common.utils.PageUtils;
import com.crr.mall.common.utils.R;


/**
 * 属性分组
 *
 * @author chenrenren
 * @email chenrenren@gmail.com
 * @date 2024-11-22 17:42:37
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController extends BaseController {
    @Autowired
    private AttrGroupService attrGroupService;

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    // @RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable(value = "catelogId") Long catelogId) {
        PageUtils page = attrGroupService.queryPage(params);
        return R.ok().put("page", page);
    }

    @GetMapping("/groupwithattr/{catelogId}")
    public ResultBean getGroupAndAttr(@PathVariable(value = "catelogId") Long catelogId) {
        List<AttrGroupEntity> attrGroupEntities = attrGroupService.queryGroupAndAttrByCatelogId(catelogId);
        return success("信息列表", attrGroupEntities);
    }

    /**
     * 查询分组关联的信息
     *
     * @param attrGroupId
     * @return
     */
    @GetMapping("/relation/{attrGroupId}")
    public ResultBean attrGroupRelation(@PathVariable(value = "attrGroupId") Long attrGroupId) {
        List<Map> list = attrGroupService.queryAttrGroupRelation(attrGroupId);
        return success("attr和group关联信息", list);
    }

    /**
     * 查询分组没有关联的信息
     *
     * @return
     */
    @PostMapping("/notrelation")
    public ResultBean attrGroupNoRelation(@RequestBody AttrGroupQuery attrGroupQuery) {
        PageUtils pageUtils = attrGroupService.queryNotRelation(BeanUtil.beanToMap(attrGroupQuery));

        return success("去除没有关联的列表", pageUtils);
    }

    /**
     * 根据catelogId和其他条件分页查询
     *
     * @return
     */
    @PostMapping("/list")
    public ResultBean queryByCatelogId(@RequestBody AttrGroupQuery attrGroupQuery) {
        PageUtils pageUtils = attrGroupService.queryByCondition(BeanUtil.beanToMap(attrGroupQuery));
        return success("分组列表", pageUtils);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{attrGroupId}")
    // @RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        return R.ok().put("attrGroup", attrGroup);
    }

    @PostMapping("/attr/relation")
    public ResultBean saveGroupInfo(@RequestBody List<AttrGroupRelationVO> attrGroupVOS) {
        int i = attrGroupService.saveGroupinfo(attrGroupVOS);
        return saveResult(i);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));
        return R.ok();
    }

    @PostMapping("/attr/relation/delete")
    public ResultBean deleteByid(@RequestBody AttrGroupRelationVO[] attrGroupVOS) {
        int i = attrGroupService.deleteByids(attrGroupVOS);
        return deleteResult(i);

    }

}
